package com.rql;

import com.rql.Room.Room;
import com.rql.Room.RoomSet;
import com.rql.article.ArmorDefense;

import com.rql.article.BareHandedAttack;
import com.rql.article.KnifeAttack;
import com.rql.creature.*;
import com.rql.handler.*;

import java.util.HashMap;
import java.util.Scanner;


/**
 * @description:
 * @author: 86133
 * @date: Created in 2020/6/3 20:50
 * @version: ${VERSION}
 * @modified By:
 */
public class Sense {
    Person person = new Person();

    MonsterRabit monsterRabit = new MonsterRabit();
    MonsterWildBoar monsterWildBoar = new MonsterWildBoar();
    MonsterCrocodile monsterCrocodile = new MonsterCrocodile();
    MonsterDragin monsterDragin = new MonsterDragin();

    MonsterSet monsterSet = new MonsterSet();
    RoomSet roomSet = new RoomSet();

    private HashMap<String, Handler> handlders =new HashMap<String, Handler>();



    public Sense(){
        handlders.put("go", new HandlerGo(this));
        handlders.put("bye", new HandlerBye(this));
        handlders.put("help",new HandlerHelp(this));
        handlders.put(("chop"),new HandlerChop(this));

        initRoom();
    }



    public void initSence(){
        initPerson();
        initMonster();
        initRoom();
    }

    public void initRoom(){
        Room room1,room2,room3,room4,room5,room6;
        room1 = new Room("r1","起点");
        room2 = new Room("r2","小镇兵器铺");
        room3 = new Room("r3","萤之森林");
        room4 = new Room("r4","暗之沼泽");
        room5 = new Room("r5","龙之山");

        room1.setExits("r2",room2);
        room2.setExits("r3",room3);
        room3.setExits("r4",room4);
        room4.setExits("r5",room5);

        roomSet.getRooms().add(room1);
        roomSet.getRooms().add(room2);
        roomSet.getRooms().add(room3);
        roomSet.getRooms().add(room4);
        roomSet.getRooms().add(room5);

        person.setCurrentRoom(room1);
    }

    public void goRoom(String direction)
    {
        Room nextRoom = person.getCurrentRoom().getExit(direction);

        if (nextRoom == null) {
            System.out.println("那里没有路哦");
        }
        else {
            person.setCurrentRoom(nextRoom);
        }
        showPromptAndProcess();
    }

    public void initPerson(){

        person.setId("001");
        person.setDescription("小花");
        person.setHp(200);
        person.setMaxhp(200);
        person.setLv(1);
        person.setExp(0);
        person.setExpVR(100);

        person.setCurrentWeapon(new ArmorDefense("A1","护甲",10));
        person.getArticleWeapons().add(0,person.getCurrentWeapon());

        person.setCurrentWeapon(new KnifeAttack("k1","宝剑",-30));
        person.getArticleWeapons().add(1,person.getCurrentWeapon());




    }

    public void initMonster(){

        monsterRabit.setId("100");
        monsterRabit.setDescription("碧眼兔");
        monsterRabit.setHp(80);
        monsterRabit.setLeftExp(80);
        monsterRabit.setCurrentRoom(roomSet.getRooms().get(0));
        monsterRabit.setCurrentWeapon(new BareHandedAttack("leg","飞腿",-30));


        monsterWildBoar.setId("101");
        monsterWildBoar.setDescription("野猪");
        monsterWildBoar.setHp(100);
        monsterWildBoar.setLeftExp(100);
        monsterWildBoar.setCurrentRoom(roomSet.getRooms().get(1));
        monsterWildBoar.setCurrentWeapon(new BareHandedAttack("201","猛冲",-50));

        monsterCrocodile.setId("102");
        monsterCrocodile.setDescription("巨鳄");
        monsterCrocodile.setHp(120);
        monsterCrocodile.setLeftExp(90);
        monsterCrocodile.setCurrentRoom(roomSet.getRooms().get(2));
        monsterCrocodile.setCurrentWeapon(new BareHandedAttack("202","铁齿钢牙",-30));

        monsterDragin.setId("103");
        monsterDragin.setDescription("恶龙");
        monsterDragin.setHp(200);
        monsterDragin.setCurrentRoom(roomSet.getRooms().get(3));
        monsterDragin.setCurrentWeapon(new BareHandedAttack("203","烈焰",-50));


        monsterSet.getMonsters().add(0, monsterRabit);
        monsterSet.getMonsters().add(1, monsterWildBoar);
        monsterSet.getMonsters().add(2, monsterCrocodile);
        monsterSet.getMonsters().add(3, monsterDragin);

    }

    public void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到拯救勇士！");
        System.out.println("逃出魔窟的公主说：勇士躺在恶龙的金币上，变成了新的巨龙");
        System.out.println("勇士的妹妹不相信，她要亲自去看看");
        System.out.println("提起她的宝剑，去往西方的高山");
        System.out.println("还有疑问？输入 help试试看！");
        System.out.println();
        showPromptAndProcess();
    }

    public void showPromptAndProcess(){
        boolean flag = false;
        System.out.println("=======================================================================================");
        System.out.print("此处的怪兽有:  ");
        for (Monster monsters: monsterSet.getMonsters()
             ) {
            if (monsters.getCurrentRoom().getId().equals(person.getCurrentRoom().getId())){
                if (monsters.getHp() <= 0) {
                    monsters.setHp(0);
                    flag = true;
                    person.getEnemyExp(monsters);
                    person.upGrade();
                }
                System.out.println(monsters.getId()+"  "+monsters.getDescription()+"  血量: "+monsters.getHp()+
                        "  攻击力: "+-monsters.getCurrentWeapon().getEffectValue());
                break;
            }
        }
        System.out.print(person.getDescription()+" 你目前在  " + person.getCurrentRoom());
        System.out.println("  你的状态: 血量: "+person.getHp()+"  等级: "+person.getLv()+"  当前经验值: "+person.getExp()+
                "  武器: "+person.getCurrentWeapon().getDescription()+
                "  攻击力: "+-person.getCurrentWeapon().getEffectValue()+
                "  防具: " +person.getArticleWeapons().get(0).getDescription()+"  防御力: "
                +person.getArticleWeapons().get(0).getEffectValue());

        if (flag)
            ProcessOptimization();
        System.out.println("==============================*==========*==========*===================================");
    }

    public void ProcessOptimization(){
            System.out.println("当前地点怪兽已死，可前往下一地点");
            System.out.print("出口有:  ");
            System.out.print(person.getCurrentRoom().getExitDesc());
            System.out.println(roomSet.getRooms().get(roomSet.getRooms().indexOf(person.getCurrentRoom())+1));
    }

    public void play(){

        Scanner in = new Scanner(System.in);
        while (true) {
            String line = in.nextLine();
            String[] words = line.split(" ");
            Handler handler = handlders.get(words[0]);
            String value = "";
            if (words.length > 1){
                value = words[1];
            }

            if (monsterSet.getMonsters().get(3).getHp() == 0 ){

                System.out.println("女孩历经磨难，还是杀死了作恶的巨龙");
                break;
            }
            else if (handler != null) {
                handler.doCmd(value);
                if (handler.isBye())
                    break;
            }
            else {
                System.out.println("输入错误命令，重新输入。");
            }
        }
        System.out.println("游戏结束");
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }



    public MonsterRabit getMonsterRabit() {
        return monsterRabit;
    }

    public void setMonsterRabit(MonsterRabit monsterRabit) {
        this.monsterRabit = monsterRabit;
    }

    public MonsterWildBoar getMonsterWildBoar() {
        return monsterWildBoar;
    }

    public void setMonsterWildBoar(MonsterWildBoar monsterWildBoar) {
        this.monsterWildBoar = monsterWildBoar;
    }

    public MonsterCrocodile getMonsterCrocodile() {
        return monsterCrocodile;
    }

    public void setMonsterCrocodile(MonsterCrocodile monsterCrocodile) {
        this.monsterCrocodile = monsterCrocodile;
    }

    public MonsterDragin getMonsterDragin() {
        return monsterDragin;
    }

    public void setMonsterDragin(MonsterDragin monsterDragin) {
        this.monsterDragin = monsterDragin;
    }

    public MonsterSet getMonsterSet() {
        return monsterSet;
    }

    public void setMonsterSet(MonsterSet monsterSet) {
        this.monsterSet = monsterSet;
    }

    public RoomSet getRoomSet() {
        return roomSet;
    }

    public void setRoomSet(RoomSet roomSet) {
        this.roomSet = roomSet;
    }

    public HashMap<String, Handler> getHandlders() {
        return handlders;
    }

    public void setHandlders(HashMap<String, Handler> handlders) {
        this.handlders = handlders;
    }
}
